﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace PLock {
	public partial class CopyDlg : Form {
		public CopyDlg() {
			InitializeComponent();

			LabelCache.CacheInvalidated += CacheInvalidatedCallback;
		}

		private void CacheInvalidatedCallback() {
			if (this.Visible)
				SetItems();
		}

		public void SetItems() {
			listBoxEntries.Items.Clear();
			listBoxEntries.Items.AddRange(LabelCache.GetLabels());
		}

		private void listBoxEntries_DoubleClick(object sender, EventArgs e) {
			if (listBoxEntries.SelectedItem == null)
				return;
			string label = listBoxEntries.SelectedItem.ToString();
			string cap = null;
			string[] caps = LabelCache.GetCaps(label);
			if (Array.IndexOf(caps, "pass") != -1)
				cap = "pass";
			else if (Array.IndexOf(caps, "user") != -1)
				cap = "user";
			else if (Array.IndexOf(caps, "snip") != -1)
				cap = "snip";
			if (cap != null) {
				DoCopy(label, cap);
			}
		}

		private void listBoxEntries_SelectedIndexChanged(object sender, EventArgs e) {
			bool enable = listBoxEntries.SelectedItem != null;
			string[] caps = null;
			if (enable)
				caps = LabelCache.GetCaps(listBoxEntries.SelectedItem.ToString());
			toolStripButtonPass.Enabled = enable && Array.IndexOf(caps, "pass") != -1;
			toolStripButtonUser.Enabled = enable && Array.IndexOf(caps, "user") != -1;
			toolStripButtonSnip.Enabled = enable && Array.IndexOf(caps, "snip") != -1;
		}

		private void DoCopy(string label, string cap) {
			if (ConfigDlg.CopyToClipboard(label, cap))
				labelStatus.Text = string.Format("Copied {0} {1} to the Clipboard.", label, ConfigDlg.CapLabel(cap));
			else
				labelStatus.Text = string.Format("{0} {1} not copied.", label, ConfigDlg.CapLabel(cap));
		}

		private void toolStripButtonPass_Click(object sender, EventArgs e) {
			if (!toolStripButtonPass.Enabled) return;
			string label = listBoxEntries.SelectedItem.ToString();
			DoCopy(label, "pass");
		}

		private void toolStripButtonUser_Click(object sender, EventArgs e) {
			if (!toolStripButtonUser.Enabled) return;
			string label = listBoxEntries.SelectedItem.ToString();
			DoCopy(label, "user");
		}

		private void toolStripButtonSnip_Click(object sender, EventArgs e) {
			if (!toolStripButtonSnip.Enabled) return;
			string label = listBoxEntries.SelectedItem.ToString();
			DoCopy(label, "snip");
		}

		private void buttonClose_Click(object sender, EventArgs e) {
			this.Hide();
			labelStatus.Text = "";
		}

		private void CopyDlg_FormClosing(object sender, FormClosingEventArgs e) {
			if (e.CloseReason == CloseReason.UserClosing) {
				e.Cancel = true;
				buttonClose_Click(sender, e);
			}
		}

		public void LoadPosition() {
			using (RegistryKey key = Registry.CurrentUser.CreateSubKey(@"Software\PLock\CopyDlg")) {
				if (key.GetValue("Left") != null)
					this.Left = (int)key.GetValue("Left");
				if (key.GetValue("Top") != null)
					this.Top = (int)key.GetValue("Top");
				if (this.Left == 0 || this.Top == 0)
					this.StartPosition = FormStartPosition.WindowsDefaultLocation;
			}
		}

		public void SavePosition() {
			using (RegistryKey key = Registry.CurrentUser.CreateSubKey(@"Software\PLock\CopyDlg")) {
				key.SetValue("Left", this.Left);
				key.SetValue("Top", this.Top);
			}
		}
	}
}
